import { list } from "postcss";
import { scientificToDecimal } from "@/comm/format";
export function getNearData(ary: any[], pot: any, threshold = 100) {
    let t = getTimer(pot.timestamp);

    const aim = ary.find((val: any, i: number) => {
        let t2 = getTimer(val.timestamp);
        // console.log(`t: ${t} t2: ${t2}`);
        // console.log(t,"val.timestamp",t2,pot.a)
        if (t == t2) {
            return true;
        }

        let distance = Math.abs(val.timestamp - pot.timestamp)
        // console.log(distance, val.timestamp , pot.timestamp," val.timestamp - pot.timestamp")

        // if (distance == 0) {
        //     return true;
        // }
        // if (distance > 0) return true;
        if (distance > 0 && distance < 5000) {
            return true
        }
        return false;
    });

    return aim;
}

export function getNearTime(ary: any[], pot: any) {
    const aim = ary.find((val: any, i: number) => {
        let distance = val.timestamp - pot.timestamp;
        if (distance > 0) return true;
        return false;
    });
    return aim;
}

export function getDayTimer(t: number) {
    if (!t) return "";
    const currentDate = new Date(t);
    const year = currentDate.getFullYear();
    const month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
    const day = ("0" + currentDate.getDate()).slice(-2);
    const hours = ("0" + currentDate.getHours()).slice(-2);
    const minutes = ("0" + currentDate.getMinutes()).slice(-2);
    const seconds = ("0" + currentDate.getSeconds()).slice(-2);

    // 构建格式化的日期时间字符串
    const formattedDateTime = `${year}-${month}-${day}`;
    return formattedDateTime;
}

export function getFormatTime(t: number, format: string) {
    if (!t) return "";
    const currentDate = new Date(t);
    const year = currentDate.getFullYear();
    const month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
    const day = ("0" + currentDate.getDate()).slice(-2);
    const hours = ("0" + currentDate.getHours()).slice(-2);
    const minutes = ("0" + currentDate.getMinutes()).slice(-2);
    const seconds = ("0" + currentDate.getSeconds()).slice(-2);
    switch (format) {
        case "yyyy":
            return `${year}`;
        case "yyyy-MM":
            return `${year}-${month}`;
        case "yyyy-MM-dd":
            return `${year}-${month}-${day}`;
        case "yyyy-MM-dd hh":
            return `${year}-${month}-${day} ${hours}`;
        case "yyyy-MM-dd hh:mm":
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        case "yyyy-MM-dd hh:mm:ss":
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
}
export function getTimer(t: number, isDay: Boolean = false) {
    if (!t) return "";
    const currentDate = new Date(t);
    const year = currentDate.getFullYear();
    const month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
    const day = ("0" + currentDate.getDate()).slice(-2);
    const hours = ("0" + currentDate.getHours()).slice(-2);
    const minutes = ("0" + currentDate.getMinutes()).slice(-2);
    const seconds = ("0" + currentDate.getSeconds()).slice(-2);

    // 构建格式化的日期时间字符串
    const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    if (isDay) {
        return `${year}-${month}-${day}`;
    } else {
        return formattedDateTime;
    }
}
/*
 传入K线数据
 返回x天内是否涨跌幅以及涨跌幅的百分比
*/
export function getKlineDayLimit(klist: any[], Day: number, timeType: string = 'H') {
    let klineXD;
    if (klist.length == 0) return 0;
    const len = timeType == 'D' ? 1 * Day : 24 * Day; //D天的数据长度
    if (timeType == 'D') {
        klineXD = len >= klist.length ? klist : klist.slice(-len); //取最后D天的数据 
    } else {
        klineXD = len >= klist.length ? klist : klist.slice(-len);
    }
    const firstOpen = +scientificToDecimal(klineXD[0].open); //第一天的开盘价
    const lastClose = +scientificToDecimal(klineXD[klineXD.length - 1].close); //最后的收盘价
    const change = lastClose - firstOpen; //收盘价 - 开盘价
    const percentageChange = (change / firstOpen) * 100; //涨跌幅 = （N天的收盘价  - N天前开盘价）/  N天前开盘价  * 100
    // console.log(`X天：${Day}  第一天收盘价：${firstOpen},最后一天价格：${lastClose} 涨幅比：${percentageChange}`);
    return percentageChange.toFixed(3);
}
/* 
  传入K线数据和当前价格
  和昨天的收盘价比较，返回涨跌幅
*/
export function getKlineNowLimit(klist: any[], timeType: string = 'H') {
    let lastDay;
    let yesterdayOpen;
    if (klist.length == 0) return 0;
    const len = timeType == 'D' ? 2 : 24; //D天的数据长度
    lastDay = klist[klist.length - 1]; //取最后一天的数据 length
    if (timeType == 'D') {
        yesterdayOpen = len >= klist.length ? +scientificToDecimal(klist[0]?.open) : +scientificToDecimal(klist[klist.length - len]?.open);
    } else {
        yesterdayOpen = len >= klist.length ? +scientificToDecimal(klist[0]?.open) : +scientificToDecimal(klist[klist.length - len]?.open);
    }
    const price = +scientificToDecimal(lastDay.close);
    const change = price - yesterdayOpen; //涨跌幅 = (当前价格 - 昨天的开盘) / 昨天的开盘 * 100%
    const percentageChange = (change / yesterdayOpen) * 100; //涨跌幅百分比
    // console.log(`24前价格：${yesterdayOpen},当前价格：${price} 涨幅比：${percentageChange}`);
    return percentageChange.toFixed(2);
}
